package com.sdry.service.impl.llm;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.ProductionLineMapper;
import com.sdry.model.llm.ProductionLine;
import com.sdry.service.llm.ProductionLineService;

/**
 * 生产线serviceImp
 * @Title:ProductionLineServiceImp
 * @Package com.sdry.service.impl.llm
 * @author llm
 * @date 2018年12月26日
 * @version 1.0
 */

@Service
public class ProductionLineServiceImp implements ProductionLineService {

	@Resource
	private ProductionLineMapper lineMapper;
	
	/**      
	 * 获取总行数 
	 * @param productionLine
	 * @return    
	 */
	@Override
	public int countProductionLine(ProductionLine productionLine) {
		return lineMapper.countProductionLine(productionLine);
	}

	/**     
	 * 分页查询生产线  
	 * @param map
	 * @return    
	 */
	@Override
	public List<ProductionLine> listPageProductionLine(Map<String, Object> map) {
		return lineMapper.listPageProductionLine(map);
	}

	/**     
	 * 保存产线信息  
	 * @param productionLine
	 * @return    
	 */
	@Override
	public int saveProductionLine(ProductionLine productionLine) {
		int inSuccess = -2;
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(date);
		productionLine.setRemark01(time);
		List<ProductionLine> line2 = lineMapper.getProductionLineByNumber(productionLine.getLineNumber());
		if(line2.size() == 0){
			int i = lineMapper.saveProductionLine(productionLine);
			if(i > 0){
				inSuccess = 2;
			}
		}
		return inSuccess;
	}

	/**     
	 * 修改产线信息    
	 * @param productionLine
	 * @return    
	 */
	@Override
	public int updateProductionLine(ProductionLine productionLine) {
		int inSuccess = -3;
		ProductionLine pro = lineMapper.getProductionLineById(productionLine.getLineId());
		if(pro.getLineNumber().equals(productionLine.getLineNumber())){
			int i = lineMapper.updateProductionLine(productionLine);
			if(i > 0){
				inSuccess = 3;
			}
		}else{
			List<ProductionLine> line2 = lineMapper.getProductionLineByNumber(productionLine.getLineNumber());
			if(line2.size() == 0){
				int i = lineMapper.updateProductionLine(productionLine);
				if(i > 0){
					inSuccess = 3;
				}
			}
		}
		return inSuccess;
	}

	/**   
	 * 删除产线信息    
	 * @param str
	 * @return    
	 */
	@Transactional(rollbackFor={Exception.class})
	public int deleteProductionLine(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String id : split) {
				isSuccess = lineMapper.deleteProductionLine(Long.parseLong(id));
			}
		}catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**      
	 * 根据名称查询产线信息 
	 * @param lineName
	 * @return    
	 */
	@Override
	public List<ProductionLine> getProductionLineByName(String lineName) {
		return lineMapper.getProductionLineByName(lineName);
	}

	/**  
	 * 根据编号查询产线信息     
	 * @param lineNumber
	 * @return    
	 */
	@Override
	public List<ProductionLine> getProductionLineByNumber(String lineNumber) {
		return lineMapper.getProductionLineByNumber(lineNumber);
	}

}
